package rexsee.cartoon;

import rexsee.core.browser.Browser;
import rexsee.core.style.RexseeDrawable;
import rexsee.core.style.StyleSheet;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class CartoonDialog extends Dialog {

	public static final String DEFAULT_WIDNOW_STYLE = "top:0;left:0;width:fillparent;height:fillparent;" +
			"background-color:transparent;alpha:1;" +
			"window-align:center;window-vertical-align:middle;" +
			"window-cancelable:true;window-outside-cancelable:false;" +
			"window-animation:none;" +
			"window-fullscreen:normal;" +
			"window-modeless:false;" +
			"window-blur-behind:false;" +
			"window-show-wallpaper:false;" +
			"window-split-screen:false;" +
			"window-dim-amount:0;" +
			"window-auto-hide:0;";

	public static final String EVENT_ONCARTOONDISMISS = "onCartoonDismiss";

	public final Browser mBrowser;
	public final StyleSheet styleSheet;
	public final FrameLayout frame;
	public final CartoonView cartoonView;
	public Parser parser = null;

	public CartoonDialog(Browser browser, String style) {

		super(browser.getContext(), android.R.style.Theme_Panel);

		mBrowser = browser;
		browser.eventList.add(EVENT_ONCARTOONDISMISS);
		styleSheet = new StyleSheet(browser.getContext()).parseStyle(DEFAULT_WIDNOW_STYLE).parseStyle(style);

		cartoonView = new CartoonView(browser.getContext());
		cartoonView.setBackgroundColor(Color.TRANSPARENT);
		cartoonView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

		frame = new FrameLayout(browser.getContext());
		frame.addView(cartoonView);
		frame.setBackgroundDrawable(RexseeDrawable.getDrawable(mBrowser, styleSheet.background_color, new ColorDrawable(Color.TRANSPARENT)));
		frame.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

		Window window = this.getWindow();
		window.requestFeature(Window.FEATURE_NO_TITLE);
		window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		if (styleSheet.window_fullscreen.equalsIgnoreCase("fullscreen")) window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		if (styleSheet.window_modeless.equals("true")) window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
		if (styleSheet.window_blur_behind.equalsIgnoreCase("true")) window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		if (styleSheet.window_show_wallpaper.equalsIgnoreCase("true")) window.setFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);

		this.setContentView(frame);
		this.setOnDismissListener(new DialogInterface.OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
				cartoonView.stop();
				if (parser != null) parser.destroy();
				mBrowser.eventList.run(EVENT_ONCARTOONDISMISS);
				System.gc();
			}
		});

		show();

		final int autoHide = Integer.parseInt(styleSheet.window_auto_hide);
		if (autoHide > 0) {
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					CartoonDialog.this.dismiss();
				}
			}, autoHide * 1000);
		}

		WindowManager.LayoutParams attr = window.getAttributes();
		attr.type = (styleSheet.window_split_screen.equalsIgnoreCase("true")) ? WindowManager.LayoutParams.TYPE_INPUT_METHOD : WindowManager.LayoutParams.TYPE_APPLICATION;
		attr.windowAnimations = styleSheet.getWindowAnimation();
		attr.alpha = styleSheet.getAlpha();
		attr.dimAmount = Float.parseFloat(styleSheet.window_dim_amount);

		attr.gravity = styleSheet.getWindowGravity();

		int top = styleSheet.getTop();
		if (top < 0) {
			top = mBrowser.application.screenHeight + top;
		}
		int left = styleSheet.getLeft();
		if (left < 0) {
			left = mBrowser.application.screenWidth + left;
		}
		if (left != 0) attr.x = left;
		if (top != 0) attr.y = top;

		attr.width = styleSheet.getWidth();
		attr.height = styleSheet.getHeight();

		window.setAttributes(attr);

		this.setCancelable(styleSheet.window_cancelable.equalsIgnoreCase("true"));
		this.setCanceledOnTouchOutside(styleSheet.window_outside_cancelable.equalsIgnoreCase("true"));

	}

	public void setParser(Parser cartooonParser) {

		mBrowser.progressDialog.hide();

		cartoonView.clear();

		parser = cartooonParser;
		if (parser == null || parser.cartoons == null) return;

		try {
			cartoonView.setInterval(parser.interval);
			cartoonView.add(parser.cartoons);
			cartoonView.setOnTouchListener(parser.touchListener);
		} catch (Exception e) {
			parser.error(e.getLocalizedMessage());
		}

		cartoonView.play();

	}

}
